package com.changgou.goods.controller;

import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping("/brand")
@RestController
public class BrandController {
    @Autowired
    private BrandService brandService;

    @GetMapping("/findAll")
    public Result<List<Brand>> findAll(){
        List<Brand> brandServiceAll = brandService.findAll();
        return new Result<List<Brand>>(true, StatusCode.OK,"查询成功",brandServiceAll);
    }

    @GetMapping("/{id}")
    public Result<Brand> findById(@PathVariable("id") Integer id){
        Brand brand = brandService.findById(id);
        return new Result<Brand>(true, StatusCode.OK,"查询成功",brand);
    }

    @PostMapping("/insert")
    public Result insert(@RequestBody Brand brand){
        brandService.insert(brand);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    @PutMapping("/update/{id}")
    public Result update(@RequestBody Brand brand,@PathVariable("id") Integer id){
        brand.setId(id);
        brandService.update(brand);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id){
        brandService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    @GetMapping("/search")
    public Result search(@RequestParam Map seachMap){
        List<Brand> search = brandService.search(seachMap);
        return new Result(true,StatusCode.OK,"查询成功",search);
    }

//    @GetMapping("/search/{pageNum}/{pageSize}")
//    public Result searchPage(@PathVariable("pageNum")int pageNum, @PathVariable("pageSize")int pageSize){
//        Page<Brand> brands = brandService.searchPage(pageNum, pageSize);
//        PageResult pageResult = new PageResult(brands.getTotal(), brands.getResult());
//        return new Result(true,StatusCode.OK,"查询成功",pageResult);
//    }

    //分页+条件查询
    //http://localhost:9011/brand/search/2/5?lettrt=I
    @GetMapping("/search/{pageNum}/{pageSize}")
    public Result searchPage(@RequestParam Map<String,String> seachMap,@PathVariable("pageNum")int pageNum, @PathVariable("pageSize")int pageSize){
        Page<Brand> brands = brandService.findPage(pageNum,pageSize,seachMap);
        PageResult pageResult = new PageResult(brands.getTotal(), brands.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }
}
